package com.web.admin.web.base.core;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.core.facade.SequenceFacade;
import com.web.base.core.message.SequenceRequest;
import com.web.base.core.message.SequenceResponse;
import com.web.core.util.Const;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

@Controller("coreSequenceController")
@Http(url = "/base/core")
public class SequenceController
{
	@Resource
	private SequenceFacade sequenceFacade;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public SequenceController()
	{
	}
	
	@Http(url = "/sequence_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		SequenceRequest sequenceRequest = request.getQueryObject(SequenceRequest.class);
		return View.createBuilder().setView("/base/core/sequence_list")
				.put("page", sequenceFacade.listPage(sequenceRequest, Const.PAGE_START, Const.PAGE_SIZE))
				.put("model", sequenceRequest)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/sequence_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		SequenceRequest sequenceRequest = request.getPostObject(SequenceRequest.class);
		int start = request.getPostInt("start", 1);
		Page<SequenceResponse> page = sequenceFacade.listPage(sequenceRequest, start, Const.PAGE_SIZE);
		page.setCurPage(request.getPostInt("start", Const.PAGE_START));
		return View.createBuilder().setView("/base/core/sequence_list_grid")
				.put("page", page)
				.build();
	}
	
	@Http(url = "/sequence_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		int id = request.getQueryInt("id", 0);
		SequenceResponse response = null;
		if (id > 0)
		{
			response = sequenceFacade.get(id);
		}
		return View.createBuilder().setView("/base/core/sequence_get")
				.put("model", response == null ? new SequenceResponse() : response)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/sequence_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		SequenceRequest sequenceRequest = request.getPostObject(SequenceRequest.class);
		if (sequenceRequest.getId() == null)
		{
			ValidationUtil.valid(sequenceRequest, SaveModel.class);
			sequenceFacade.add(sequenceRequest);
		}
		else
		{
			ValidationUtil.valid(sequenceRequest, UpdateModel.class);
			sequenceFacade.update(sequenceRequest);
		}
	}
	
}
